﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using mfe.bll;
using System.Collections.Specialized;

namespace mfe_proj.Forms
{
    public delegate void NavigationFormEventHandler(object sender, NavigationFormEventArgs e);
    public partial class NavigationForm : Form
    {
        public new event NavigationFormEventHandler DoubleClick;
        public event NavigationFormEventHandler NavigationLoaded;

        public string SelectedGuidID
        {
            get
            {
                if (listView1.SelectedItems.Count == 0)
                    return string.Empty;
                return listView1.SelectedItems[0].Name;
            }
        }

        public NavigationForm()
        {
            InitializeComponent();
            ToolStripMenuItem mnuAll = new ToolStripMenuItem("All");
            mnuAll.Tag = 0;
            mnuAll.Click += new EventHandler(FilterMenu_Click);
            btnFilter.DropDownItems.Add(mnuAll);
            foreach (string type in Enum.GetNames(typeof(Enums.ContentType)))
            {
                ToolStripMenuItem mnu = new ToolStripMenuItem(type);
                mnu.Tag = (int)((Enums.ContentType)Enum.Parse(typeof(Enums.ContentType), type, true));
                mnu.Click += new EventHandler(FilterMenu_Click);
                btnFilter.DropDownItems.Add(mnu);
            }
        }

        void FilterMenu_Click(object sender, EventArgs e)
        {
            Program.ItemCollection.Clear();
            Program.ItemCollection = null;
            LoadData((int)((ToolStripMenuItem)sender).Tag);
        }

        public Panel DockablePanel
        {
            get { return panelDockable; }
            set { panelDockable = value; }
        }

        public void RemoveFromTree()
        {
            //treeView1.SelectedNode.Remove();
            listView1.SelectedItems[0].Remove();
        }

        public void LoadData(int type)
        {
            if (Program.ItemCollection == null)
            {
                StringCollection fields = new StringCollection();
                StringCollection conditions = new StringCollection();
                fields.Add("ItemID");
                fields.Add("ItemGuidID");
                fields.Add("ContentType");
                fields.Add("Title");
                //conditions.Add("ShowInNav=1");
                if (type > 0)
                    conditions.Add("ContentType=" + type);
                Program.ItemCollection = Item.ItemCollection(fields, conditions);
            }
            listView1.Items.Clear();
            listView1.BeginUpdate();
            listView1.Columns[0].Width = this.Width;
            foreach (Item item in Program.ItemCollection)
            {
                ListViewItem lvi = new ListViewItem(item.Title);
                lvi.Name = item.ItemGuidID;
                switch ((Enums.ContentType)item.ContentType)
                {
                    case Enums.ContentType.Author:
                        lvi.ImageIndex = (int)Enums.FilterIcons.Author;
                        break;
                    case Enums.ContentType.Detective:
                        lvi.ImageIndex = (int)Enums.FilterIcons.Detective;
                        break;
                    case Enums.ContentType.Book:
                        lvi.ImageIndex = (int)Enums.FilterIcons.Book;
                        break;
                }
                listView1.Items.Add(lvi);
            }
            listView1.EndUpdate();
            //treeView1.Nodes.Clear();
            //treeView1.BeginUpdate();
            //foreach (Item item in Program.ItemCollection)
            //{
            //    treeView1.Nodes.Add(item.ItemGuidID, item.Title);
            //}
            //treeView1.EndUpdate();

            ///TODO: select the first node to display?
            

            if (NavigationLoaded != null) NavigationLoaded(this, new NavigationFormEventArgs(Program.ItemCollection.Count));
        }

        //private void treeView1_DoubleClick(object sender, EventArgs e)
        //{
        //    if (treeView1.SelectedNode == null) return;
        //    if (DoubleClick != null)
        //        DoubleClick(sender, new NavigationFormEventArgs(treeView1.SelectedNode.Name));
        //}

        //private void treeView1_KeyDown(object sender, KeyEventArgs e)
        //{
        //    if (e.KeyCode == Keys.Enter)
        //        treeView1_DoubleClick(sender, null);
        //}

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0) return;
            if (DoubleClick != null)
                DoubleClick(sender, new NavigationFormEventArgs(listView1.SelectedItems[0].Name));
        }

        private void listView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                listView1_DoubleClick(sender, null);
        }
    }

    public class NavigationFormEventArgs : EventArgs
    {
        string _ItemGuidID;
        int _Count;
        public string ItemGuidID { get { return _ItemGuidID; } set { _ItemGuidID = value; } }
        public int Count { get { return _Count; } set { _Count = value; } }
        public NavigationFormEventArgs(string itemGuidID)
        {
            _ItemGuidID = itemGuidID;
        }
        public NavigationFormEventArgs(int count)
        {
            _Count = count;
        }
    }
}
